package com.example.studentstatusmanagement.service;

import com.example.studentstatusmanagement.mapper.MajorMapper;
import com.example.studentstatusmanagement.pojo.Major;
import com.example.studentstatusmanagement.pojo.MajorExample;
import com.example.studentstatusmanagement.pojo.doo.ReqMajor;
import com.example.studentstatusmanagement.pojo.dto.MajorInfo;
import com.example.studentstatusmanagement.pojo.dto.SMajor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Resource
    private MajorMapper majorMapper;

    @Autowired
    private ClassroomService classroomService;

    // 查询未删除的majors
    @Override
    public List<MajorInfo> selectByMajors() {

        return majorMapper.selectMajors();
    }

    @Override
    public List<SMajor> selectMajorsInfo(ReqMajor reqMajor) {
        return majorMapper.selectMajorsInfo(reqMajor);
    }

    // 返回menu
    @Override
    public List<MajorInfo> selectMajorsAndClassroomMenu() {
        return addClassrooms();
    }

    @Override
    public int updateByPrimaryKeySelective(Major record) {
        return majorMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int insertSelective(Major record) {
        return majorMapper.insertSelective(record);
    }

    @Override
    public List<Major> selectByExample(MajorExample example) {
        return majorMapper.selectByExample(example);
    }

    @Override
    public List<ReqMajor> selectMajorsforClass() {
        return majorMapper.selectMajorsforClass();
    }

    // 将查询出的子菜单与父菜单并和
    private List<MajorInfo> addClassrooms() {

        List<MajorInfo> majorInfoList = new ArrayList<>();

        for (int i = 0; i < selectByMajors().size(); i++) {

            MajorInfo majorinfo = selectByMajors().get(i);
            majorinfo.setChildren(classroomService.selectClassroomsByMajorId(majorinfo.getId()));
            majorInfoList.add(majorinfo);

        }
        return majorInfoList;
    }

}
